/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By 深圳中科鑫智科技有限公司
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.business.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shop.cereshop.business.annotation.NoRepeatSubmit;
import com.shop.cereshop.business.annotation.NoRepeatWebLog;
import com.shop.cereshop.business.dao.buyer.CereBuyerUserDAO;
import com.shop.cereshop.business.page.buyer.BuyerUser;
import com.shop.cereshop.business.page.buyer.BuyerUserDetail;
import com.shop.cereshop.business.param.buyer.*;
import com.shop.cereshop.business.service.business.CereBusinessBuyerUserService;
import com.shop.cereshop.business.service.buyer.CereBuyerUserService;
import com.shop.cereshop.business.service.label.CereBuyerShopLabelService;
import com.shop.cereshop.business.service.label.CereShopUserLabelService;
import com.shop.cereshop.business.service.order.CereShopOrderService;
import com.shop.cereshop.business.utils.ContextUtil;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.constant.IntegerEnum;
import com.shop.cereshop.commons.domain.business.CereBusinessBuyerUser;
import com.shop.cereshop.commons.domain.business.CerePlatformBusiness;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.label.CereShopUserLabel;
import com.shop.cereshop.commons.domain.order.CereShopOrder;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import com.shop.cereshop.commons.utils.GsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 客户管理
 */
@RestController
@RequestMapping("buyer")
/**
 * 注解方式生成日志对象，指定topic生成对象类名
 */
@Slf4j(topic = "BuyerUserController")
@Api(value = "客户管理模块", tags = "客户管理模块")
public class BuyerUserController {

    @Autowired
    private CereBuyerUserService cereBuyerUserService;

    @Autowired
    private CereBuyerShopLabelService cereBuyerShopLabelService;

    @Autowired
    private CereShopUserLabelService cereShopUserLabelService;

    @Autowired
    private CereBuyerUserDAO cereBuyerUserDAO;

    @Autowired
    private CereBusinessBuyerUserService cereBusinessBuyerUserService;


    @Autowired
    private CereShopOrderService cereShopOrderService;
    /**
     * 客户列表
     * @param param
     * @return
     */
    @PostMapping(value = "getAll")
    @ApiOperation(value = "客户列表")
    public Result<Page<BuyerUser>> getAll(@RequestBody BuyerUserGetAllParam param,HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
//        Long shopId = ContextUtil.getShopId();
        param.setShopId(user.getShopId());
        Page page=cereBuyerUserService.getAll(param);
        return new Result(page);
    }

    /**
     * 商家标签列表
     * @return
     */
    @PostMapping(value = "getLabels")
    @ApiOperation(value = "商家标签列表")
    public Result<List<CereShopUserLabel>> getLabels(HttpServletRequest request) throws CoBusinessException {
        //获取当前登录账户
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        List<CereShopUserLabel> list=cereShopUserLabelService.getLabels(user.getShopId());
        return new Result(list);
    }

    /**
     * 加标签
     * @param group
     * @return
     */
    @PostMapping(value = "saveLabel")
    @NoRepeatSubmit
    @ApiOperation(value = "加标签")
    @NoRepeatWebLog
    public Result saveLabel(@RequestBody BuyerUserSaveParam group, HttpServletRequest request) throws CoBusinessException{
        //获取当前登录账户
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        cereBuyerShopLabelService.saveLabel(group,user);
        return new Result(user.getUsername(),"加标签", GsonUtil.objectToGson(group));
    }

    /**
     * 添加客户
     * @param param
     * @return
     */
    @PostMapping(value = "save")
    @NoRepeatSubmit
    @ApiOperation(value = "添加客户")
    @NoRepeatWebLog
    public Result save(@RequestBody UserSaveParam param, HttpServletRequest request) throws CoBusinessException{
        //获取当前登录账户
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        Long shopId = ContextUtil.getShopId();
        cereBuyerUserService.save(param,user);
        return new Result(user.getUsername(),"添加客户", GsonUtil.objectToGson(param));
    }

    /**
     * 修改客户
     * @param param
     * @return
     */
    @PostMapping(value = "update")
    @NoRepeatSubmit
    @ApiOperation(value = "修改客户")
    @NoRepeatWebLog
    public Result update(@RequestBody UserUpdateParam param, HttpServletRequest request) throws CoBusinessException{
        //获取当前登录账户
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        Long shopId = ContextUtil.getShopId();
        param.setShopId(shopId);
        cereBuyerUserService.update(param,user);
        return new Result(user.getUsername(),"修改客户", GsonUtil.objectToGson(param));
    }

//    /**
//     * 加入黑名单
//
//     * @return
//     */
//    @GetMapping(value = "joinBacklist/{}")
//    @NoRepeatSubmit
//    @ApiOperation(value = "加入黑名单")
//    @NoRepeatWebLog
//    public Result joinBacklist(HttpServletRequest request) throws CoBusinessException{
//        //获取当前登录账户
//        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
//        Long shopId = ContextUtil.getShopId();
//        param.setShopId(shopId);
//        cereBuyerUserService.update(param,user);
//        return new Result(user.getUsername(),"修改客户", GsonUtil.objectToGson(param));
//    }

    /**
     * 删除客户
     */
    @GetMapping(value = "remove")
    @NoRepeatSubmit
    @ApiOperation(value = "删除客户")
    @NoRepeatWebLog
    public Result remove(Long buyerUserId, HttpServletRequest request) throws CoBusinessException{
        //获取当前登录账户
        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
        Long shopId = ContextUtil.getShopId();

        LambdaQueryWrapper<CereShopOrder> q1 = new LambdaQueryWrapper<>();
        Integer[] stateArr = {IntegerEnum.ORDER_PUT_UP.getCode(),
                            IntegerEnum.ORDER_WAIT_EXEC.getCode(),
                            IntegerEnum.ORDER_STAY_PAY.getCode(),
                            IntegerEnum.ORDER_STAY_DILEVERY.getCode(),
                            IntegerEnum.ORDER_HAVE_DILEVERY.getCode()};
        q1.in(CereShopOrder::getState, stateArr);
        q1.eq(CereShopOrder::getBuyerUserId,buyerUserId);

        int count = cereShopOrderService.count(q1);
        if (count != 0){
            throw new CoBusinessException(CoReturnFormat.ORDER_NOT_EXEC);
        }
        cereBuyerUserService.removeById(buyerUserId);
        cereBusinessBuyerUserService.remove(new LambdaQueryWrapper<CereBusinessBuyerUser>().eq(CereBusinessBuyerUser::getShopId,shopId).eq(CereBusinessBuyerUser::getBuyerUserId,buyerUserId));
        return new Result(1,CoReturnFormat.SUCCESS);
    }
    /**
     * 客户详情查询
     * @return
     */
    @GetMapping(value = "getById")
    @ApiOperation(value = "客户详情查询")
    public Result<BuyerUserDetail> getById(Long buyerUserId ,String phone,HttpServletRequest request) throws CoBusinessException {
//        //获取当前登录账户
//        CerePlatformBusiness user = (CerePlatformBusiness) request.getAttribute("user");
//        Long shopId = null;
//        if (user != null) {
//            shopId = user.getShopId();
//        }
        Long shopId = ContextUtil.getShopId();
        BuyerUserGetAllParam buyerUserGetAllParam = new BuyerUserGetAllParam();
        buyerUserGetAllParam.setShopId(shopId);
        buyerUserGetAllParam.setBuyerUserId(buyerUserId);
        buyerUserGetAllParam.setPhone(phone);
        List<BuyerUser> buyerUsers = cereBuyerUserDAO.getAll(buyerUserGetAllParam);
        BuyerUser buyerUser = null;
        if (CollectionUtil.isNotEmpty(buyerUsers)){
            buyerUser = buyerUsers.get(0);
        }
        return new Result(buyerUser,CoReturnFormat.SUCCESS);
    }

    @GetMapping("initBusinessBuyerUser")
    @ApiOperation("初始化成为商家客户的时间")
    public Result<Boolean> initBusinessBuyerUser() {
        cereBuyerUserService.initBusinessBuyerUser();
        return new Result(true, CoReturnFormat.SUCCESS);
    }
}
